<?
$pageid = "5";
$accessid = "a00";
$title = "Import CSV";
require("required/dbconnect.php");
require("required/function.php");

if (isset($_POST['process_flag'])) {

    $group_name = $_POST['group_name'];
    $group_id = $_POST['group_id'];
    $courier_id = $_POST['courier_id'];

    $handle = fopen('temp/csvrecords.csv', 'r');

    $err_list[] = array();
    $valid_pcode = 0;
    $filtered = 0;
    $total_col = 0;
    $now_col = 1;
    $zone_arr = array();
    $inserted=0;
    $updated=0;

    while (($data = fgetcsv($handle, 1000, ',', '"')) !== FALSE) {
        $weight = mysql_real_escape_string(trim($data[0]));
        if ($weight == 'weight') {

            while (!empty($data[$now_col]) && $data[$now_col] != "") {
                $total_col++;

                $chk_zone = mysql_query("select * from os_shipping_zone where zone_name='" . $data[$now_col] . "' and courier_id='$courier_id'") or die(mysql_error());

                if (mysql_num_rows($chk_zone) > 0) {
                    $zone_id = mysql_result($chk_zone, 0, "id");
                    $zone_arr[] = $zone_id;
                } else {
                    mysql_query("insert into os_shipping_zone(zone_name,courier_id,group_id)values('" . $data[$now_col] . "','$courier_id','$group_id')");
                    $zone_id = mysql_insert_id();
                    $zone_arr[] = $zone_id;
                }

                $now_col++;
            }
        } else {

            $weight = $data[0];
            //echo count($zone_arr);
            for ($i = 0; $i < count($zone_arr); $i++) {
                $chk = mysql_query("select * from os_shipping_zone_rate where zone_id='" . $zone_arr[$i] . "' and weight='$weight'") or die(mysql_error());

                if (mysql_num_rows($chk) > 0) {
                    $rate_id = mysql_result($chk, 0, "id");
                    mysql_query("update os_shipping_zone_rate set price='" . $data[$i + 1] . "' where id='$rate_id' ");
                    $updated++;
                } else {

                    $sql = "insert into os_shipping_zone_rate(zone_id,weight,price,group_id)values('" . $zone_arr[$i] . "','" . $weight . "','" . $data[$i + 1] . "','$group_id')";
                    mysql_query($sql);
                    $inserted++;
                }
            }
        }



        /*
          $state = mysql_real_escape_string($data[1]);
          $area_type = mysql_real_escape_string($data[2]);
          $zone = mysql_real_escape_string($data[3]);
          $weight = mysql_real_escape_string($data[4]);
          $ups_rate = mysql_real_escape_string($data[5]);
          $fedex_rate = mysql_real_escape_string($data[6]);
          $dhl_rate = mysql_real_escape_string($data[7]);

          $get_country = mysql_query("select * from os_country where engname='" . $country . "'");

          if (mysql_num_rows($get_country) == 0) {
          mysql_query("insert into os_country(engname,area_type,zone,group_id,status)values('" . $country . "','$area_type','$zone','$group_id',1)") or die(mysql_error());
          $country_id = mysql_insert_id();
          } else {
          $country_id = mysql_result($get_country, 0, "id");
          }

          if ($state != "") {

          $split_state = explode(",", $state);

          foreach ($split_state as $state_name) {
          $get_state = mysql_query("select * from os_state where name_eng='" . $state_name . "' and country_id='$country_id'");

          if (mysql_num_rows($get_state) == 0) {
          mysql_query("insert into os_state(name_eng,country_id,group_id)values('" . $state_name . "','$country_id','$group_id')") or die(mysql_error());
          }
          }
          }


          $chk_exist = mysql_query("select count(*) as result from os_shipping_rate where country_id='$country_id' and weight='$weight'")or die(mysql_error());

          if (mysql_result($chk_exist,0,"result") == 0) {
          $query = "insert into os_shipping_rate(country_id,weight,price,shipping_company_id,group_id)
          values('$country_id','$weight','$ups_rate','" . SHIPPING_COMPANY_UPS . "','$group_id'),
          ('$country_id','$weight','$dhl_rate','" . SHIPPING_COMPANY_DHL . "','$group_id'),
          ('$country_id','$weight','$fedex_rate','" . SHIPPING_COMPANY_FEDEX . "','$group_id')";
          $query_exe = mysql_query($query) or die(mysql_error() . "<br>" . $query);

          $valid_pcode++;
          }else{
          $filtered++;
          }
         * 
         */
    }


    unlink('temp/csvrecords.csv');
    header("Location:shipping_import4.php?inserted=$inserted&updated=$updated&courier_id=$courier_id&group_id=$group_id");
    
} else {



    require("required/header.php");

    $group_name = $_GET['group_name'];
    $courier_id = $_GET['courier_id'];
    $group_id = $_GET['group_id'];
    ?>	
    <script src="required/js/jquery-1.4.2.js" type="text/javascript"></script>
    <script src="required/js/blockui.js" type="text/javascript"></script>
    <script >
        function submitFrm()
        {
            $.blockUI({ message: '<h1><img src="images/loading.gif" /> Data Processing, Please wait...</h1>' });
            document.frm1.submit();
        }
    </script>

    <table width="100%" cellpadding="0" cellspacing="0" height="100%">

        <td valign="top" align="left">
            <table width="95%" align="center">
                <tr>
                    <td class="title"><?= $title ?></td>
                </tr>
                <tr>
                    <td>
                        <form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST" name="frm1">                

                            <font class="dw_info">Data Processing...</font>	
                            <input type="hidden" name="process_flag" value="1"  />
                            <input type="hidden" name="group_name" value="<?= $group_name ?>"  />
                            <input type="hidden" name="group_id" value="<?= $group_id ?>"  />
                            <input type="hidden" name="courier_id" value="<?= $courier_id ?>"  />

                        </form>
                    </td>
                </tr>
            </table>
            <script>
                submitFrm();
            </script>
        </td>
    </table>					
    <?
    require("required/footer.php");
}
?>		

